package org.zxyj.web.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zxyj.core.constants.Constants;
import org.zxyj.core.constants.Keys;
import org.zxyj.core.entity.OrderParam;
import org.zxyj.core.entity.Pagination;
import org.zxyj.core.entity.Result;
import org.zxyj.core.errorno.SystemErrorNo;
import org.zxyj.core.exception.ServiceException;
import org.zxyj.core.exception.UnifyException;
import org.zxyj.core.web.controller.BaseController;
import org.zxyj.system.domain.User;
import org.zxyj.system.server.IUserService;

/**
 * @author zhaoyong
 * @version 2016年2月26日
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	private IUserService userService;

	@RequiresPermissions("MENU:user")
	@RequestMapping({ "", "/", "/index" })
	public ModelAndView index(HttpServletRequest request, ModelAndView mv, User user, OrderParam orderParam) {
		mv.setViewName(Views.INDEX_VIEW);
		Pagination pagination = (Pagination) request.getAttribute("pagination");
		Map<String, Object> criteria = new HashMap<String, Object>();
		
		//增加排序功能
		criteria.put("orderParam", orderParam);
				
		criteria.put("username", user.getUsername());
		Pagination paginationUser = userService.listEntities(criteria, pagination);
		mv.addObject(Keys.KEY_PAGINATION, paginationUser);
		return mv;
	}

	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request, ModelAndView mv, User user) {
		mv.setViewName(Views.ADD_VIEW);
		mv.addObject(Keys.KEY_PARAM, user);
		return mv;
	}

	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request, ModelAndView mv, User user) {
		if (null == user || null == user.getId()) {
			throw new UnifyException(SystemErrorNo.DATA_NOT_EXIST);
		}
		mv.setViewName(Views.EDIT_VIEW);

		Map<String, Object> criteria = new HashMap<String, Object>();
		criteria.put("id", user.getId());
		user = userService.findEntity(criteria);
		if (null == user || null == user.getId()) {
			throw new UnifyException(SystemErrorNo.DATA_NOT_EXIST);
		}
		mv.addObject(Keys.KEY_PARAM, user);
		return mv;
	}

	@ResponseBody
	@RequestMapping("/edit/exec")
	public Result<?> doEdit(HttpServletRequest request, User user) {
		Result<?> result = new Result<String>();
		try {
			// TODO： 数据校验
			userService.persistentEntity(user);
			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}

	@ResponseBody
	@RequestMapping("/delete/exec")
	public Result<?> doDelete(HttpServletRequest request, User user) {
		Result<?> result = new Result<String>();
		try {
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("id", user.getId());
			userService.deleteEntity(criteria);

			result.setResultCode(Constants.Common.DEFAULT_RESULT_CODE);
		} catch (ServiceException e) {
			result.setResultCode(e.getErrorNo());
		}
		return result;
	}

	class Views {

		/**
		 * index视图
		 */
		public static final String INDEX_VIEW = "/user/list";

		/**
		 * 列表视图
		 */
		public static final String LIST_VIEW = "/user/list";

		/**
		 * 添加视图
		 */
		public static final String ADD_VIEW = "/user/edit";

		/**
		 * 编辑视图
		 */
		public static final String EDIT_VIEW = "/user/edit";

	}

}